<?php

    class PageController extends WebController
    {
        /**
        * Declares class-based actions.
        */
        public function actions()
        {
            return array(
                // captcha action renders the CAPTCHA image displayed on the contact page
                'captcha'=>array(
                    'class'=>'CCaptchaAction',
                    'backColor'=>0xFFFFFF,
                ),
                // page action renders "static" pages stored under 'protected/views/site/pages'
                // They can be accessed via: index.php?r=site/page&view=FileName
                'page'=>array(
                    'class'=>'CViewAction',
                ),
            );
        }
        public function filters()
        {
            return array(
                'accessControl', // perform access control for CRUD operations
            );
        }

        //        public function accessRules()
        //        {
        //            return array(
        //                array('allow',
        //                    'actions'=>array('index','contact', 'about', 'price','help', 'sitemap'),
        //                    'users'=>array('*'),
        //                ),
        //                array('deny',  // deny all users
        //                    'users'=>array('*'),
        //                ),
        //            );
        //        }

        /**
        * This is the action to handle external exceptions.
        */
        public function actionError()
        {
            if($error=Yii::app()->errorHandler->error)
            {
                if(Yii::app()->request->isAjaxRequest)
                    echo $error['message'];
                else{

                    $view = 'error';
                    if(in_array($error['code'], array(404))){
                        $view .= $error['code'];
                    }
                    $this->render($view, $error);
                }     
            }
        }

        private function _hmac_validate_checksum(&$args, $secure_code){
            if(empty($args['checksum']))
                show_error('checksum is missing');

            if(!$secure_code)
                show_error('secure code missing while validating checksum');

            $checksum = $args['checksum'];
            unset($args['checksum']);
            ksort($args);

            if(strcasecmp($checksum,hash_hmac('SHA1',implode('',$args),$secure_code))===0)
                return TRUE;
            else
                return FALSE;
        }

        // http://giatlavui.com/web/page/saveSmsNotice
        public function actionSaveSmsNotice($status, $time, $checksum){
            $secure_code = 'FHGDHFGSKEYVHBDS';
            $args = array(
                'status' => $status,
                'time' => $time,
                'checksum' => $checksum,
            );
            $check = $this->_hmac_validate_checksum($args, $secure_code);
            if($check){
                $smsNotice = new SmsNotice;
                $smsNotice->status = $status;    
                $smsNotice->time = $time;    
                $smsNotice->checksum = $checksum;
                $smsNotice->setIsNewRecord(true);
                $smsNotice->insert();
                echo '0';
                die;
            }
            echo '-1';
        }


        public function actionIndex(){
            $this->menu_selected = 'home'; 

            $branches = Branch::model()->findAllByAttributes(array('status' => 'ENABLE'), new CDbCriteria(array('order' => '`index`')));     
            $this->render('index', compact('branches'));
        }

        public function actionAbout(){
            $this->menu_selected = 'about'; 

            $this->render('about');
        }

        public function actionPrice(){
            $this->menu_selected = 'price';

            $this->render('price');
        }

        public function actionHelp(){ 
            $this->menu_selected = 'help'; 
            $qnas = Qna::model()->findAll(new CDbCriteria(array('order' => '`index` ASC')));

            $this->render('help', compact('qnas'));
        }

        public function actionContact(){
            $this->menu_selected = 'contact';
            $branches = Branch::model()->findAllByAttributes(array('status' => 'ENABLE'), new CDbCriteria(array('order' => '`index`')));       
            $this->render('contact', compact('branches'));
        }

        public function actionSitemap(){      
            $this->render('sitemap');
        }

    }
